using Microsoft.AspNetCore.Mvc;
using MediatR;
using B.S.BasicData.EroorCode;
using B.S.BasicData.Domain;

namespace B.S.BasicData.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SlaughterEnterpriseInfoController : ControllerBase
    {
        private readonly IMediator _mediator;

        public SlaughterEnterpriseInfoController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 根据编号查询屠宰企业信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>屠宰企业信息</returns>
        [HttpGet]
        public async Task<IActionResult> GetById([FromQuery] int id)
        {
            if (id <= 0)
            {
                return BadRequest(ApiResult<SlaughterEnterpriseInfo>.Fail("无效的编号", ApiResultCode.Error));
            }

            var request = new SlaughterEnterpriseInfoGetByIdRequest { Id = id };
            var result = await _mediator.Send(request);
            return Ok(result);
        }

        /// <summary>
        /// 根据公司地址查询屠宰企业信息（省市区三级联动查询）
        /// </summary>
        /// <param name="request">地址查询请求</param>
        /// <returns>屠宰企业信息列表</returns>
        [HttpPost]
        public async Task<IActionResult> QueryByAddress([FromBody] SlaughterEnterpriseInfoQueryByAddressRequest request)
        {
            if (request == null)
            {
                return BadRequest(ApiResult<List<SlaughterEnterpriseInfo>>.Fail("请求参数不能为空", ApiResultCode.Error));
            }

            var result = await _mediator.Send(request);
            return Ok(result);
        }

        /// <summary>
        /// 根据省份查询屠宰企业信息
        /// </summary>
        /// <param name="province">省份</param>
        /// <returns>屠宰企业信息列表</returns>
        [HttpGet]
        public async Task<IActionResult> QueryByProvince([FromQuery] string province)
        {
            if (string.IsNullOrWhiteSpace(province))
            {
                return BadRequest(ApiResult<List<SlaughterEnterpriseInfo>>.Fail("省份不能为空", ApiResultCode.Error));
            }

            var request = new SlaughterEnterpriseInfoQueryByAddressRequest { Province = province };
            var result = await _mediator.Send(request);
            return Ok(result);
        }

        /// <summary>
        /// 根据城市查询屠宰企业信息
        /// </summary>
        /// <param name="city">城市</param>
        /// <returns>屠宰企业信息列表</returns>
        [HttpGet]
        public async Task<IActionResult> QueryByCity([FromQuery] string city)
        {
            if (string.IsNullOrWhiteSpace(city))
            {
                return BadRequest(ApiResult<List<SlaughterEnterpriseInfo>>.Fail("城市不能为空", ApiResultCode.Error));
            }

            var request = new SlaughterEnterpriseInfoQueryByAddressRequest { City = city };
            var result = await _mediator.Send(request);
            return Ok(result);
        }

        /// <summary>
        /// 修改屠宰企业信息
        /// </summary>
        /// <param name="request">修改请求</param>
        /// <returns>修改结果</returns>
        [HttpPut]
        public async Task<IActionResult> Update([FromBody] SlaughterEnterpriseInfoUpdateRequest request)
        {
            if (request == null)
            {
                return BadRequest(ApiResult<SlaughterEnterpriseInfo>.Fail("请求参数不能为空", ApiResultCode.Error));
            }

            var result = await _mediator.Send(request);
            return Ok(result);
        }
    }
} 